<?php

/**
 * Autoloading classes on-call
 *
 * @param	string	$class_name
 * @return	boolean
 */
function __autoload($class_name)
{
	//print "Autoloading $class_name -> ";

	$namespace = array_filter(explode("\\", $class_name));
	$class_name = array_pop($namespace);

	$namespace = array_values($namespace);

	$namespacesArray = $namespace;

	if ($namespace[0] == "interfaces")
	{
		$classType = "interface";
	}
	else
	{
		$classType = "class";
	}

	$namespace = implode("\\", $namespace);

	$includeFile = "classes/".implode("/", $namespacesArray) . "/" . $class_name . "." . $classType.".php";

	$projectFile = _PROJECT_DIR . $includeFile;
	$systemFile = _SYSTEM_DIR . $includeFile;


	//Force fully qualificate namespaces
	if ($namespace[0] !== "\\")
	{
		$namespace = "\\".$namespace;
	}


	if (file_exists($projectFile))
	{
		$includeFile = $projectFile;
	}
	elseif (file_exists($systemFile))
	{
		$includeFile = $systemFile;
	}
	else
	{
		die("Filename ./$includeFile not found");
	}

	$func = $classType."_exists";

	require_once $includeFile;

	if ($func($namespace."\\".$class_name, false))
	{
		return true;
	}
	else
	{
		die("couldn't load $classType $namespace$class_name from $includeFile");
	}

	return true;
}